//
// Created by zhupi on 2024/11/2.
//

#include "project_manager_menu.h"

// project manager
#include "properties_dialog_view_ctrl.h"

#include "edit_library_dialog_view_ctrl.h"


namespace {
    const std::string  Separator_type = "-------";
}
void ProjectManagerMenu::InitProject()
{
    // 定义一个函数类型
//    using MyFunctionType = std::function<void(const QString&, int)>;

    std::vector< std::pair<std::string, MyFunctionType>> funcVec;

//    MyFunctionType boundLambda = std::bind(&ProjectManagerMenu::Separator, &ProjectManagerMenu::GetIns(),std::placeholders::_1, std::placeholders::_2);
//    MyFunctionType pro = std::bind(&PropertiesDialogViewCtrl::Initial, &PropertiesDialogViewCtrl::GetIns(), std::placeholders::_1, std::placeholders::_2);
//    MyFunctionType pro1 = std::bind( &MaterialDialogViewCtrl::Initial, &MaterialDialogViewCtrl::GetIns(), std::placeholders::_1, std::placeholders::_2);

    std::function<void(const QString &name, int parentId)> boundLambda =  [&](const QString &name, int parentId) {
//        MaterialDialogViewCtrl::GetIns().Initial(name, parentId);
    };

    std::function<void(const QString &name, int parentId)> pro1 =  [&](const QString &name, int parentId) {
        MaterialDialogViewCtrl::GetIns().Initial(name, parentId);
    };

    std::function<void(const QString &name, int parentId)> func1 =  [&](const QString &name, int parentId) {
            PropertiesDialogViewCtrl::GetIns().Initial(name, parentId);
    };
    // 将包装后的函数对象放入向量中
    funcVec.emplace_back(std::make_pair("Project Variables", func1));
    funcVec.emplace_back(std::make_pair(Separator_type, boundLambda));
    funcVec.emplace_back(std::make_pair("edit library", pro1));

    funcMap_[DataType::ProjectManager] = funcVec;
}

void ProjectManagerMenu::InitMaterial() {

}

void ProjectManagerMenu::Init()
{
    InitProject();
    InitMaterial();
}

ProjectManagerMenu &ProjectManagerMenu::GetIns() {
    static ProjectManagerMenu ins;
    return ins;
}

void ProjectManagerMenu::GetAutoMenu(int parentId, DataType type, QMenu &menu)
{
    if (!funcMap_.count(type)) {
        return;
    }
    auto funcVec = funcMap_[type];
    for (const auto &it :funcVec) {
        if (it.first == Separator_type) {
            menu.addSeparator();
        } else {
            QAction *action = new QAction(it.first.c_str(), &menu);
            menu.addAction(action);
            if (it.second != nullptr) {
                connect(action, &QAction::triggered, [&](bool istrue) {
                    //     using MyFunctionType = std::function<void(const QString&, int)>;
                    std::function<void(const QString &name, int parentId)> func = it.second;
//                    auto func = it.second;
                    func("dsafdaf", 123);
                });
            }

        }
    }
}

void ProjectManagerMenu::Separator(const QString &name, int projectId) {

}
